<template>
	<view class="wrap">
		<u-form :model="dataForm" ref="uForm">
			<u-form-item label-position="left" label="服务选择:" prop="type" label-width="20%">
				<text v-if="!dataForm.typeName" @click="typeShow = true;">请选择预约服务</text>
				<text v-else @click="typeShow = true;">{{dataForm.typeName}}</text>
				<u-picker mode="selector" v-model="typeShow"  :default-selector="[0]" :range="serviceTypeOptions" range-key="typeName" @confirm="typeConfirm"></u-picker>
				<u-icon slot="right" name="arrow-right" @click="typeShow = true;"></u-icon>
			</u-form-item>
			<u-form-item label-position="left" label="预约时间:" prop="time" label-width="20%">
				<text v-if="!dataForm.appointmentTime" @click="timeShow = true;">请选择预约时间</text>
				<text v-else @click="timeShow = true;">{{dataForm.appointmentTime}}</text>
				<u-picker mode="time" v-model="timeShow" :params="params" @confirm="timeConfirm"></u-picker>
				<u-icon slot="right" name="arrow-right" @click="timeShow = true;"></u-icon>
			</u-form-item>
			<u-form-item label-position="left" label="服务备注:" prop="intro" label-width="20%">
				<u-input type="textarea" :border="true" placeholder="请填写服务描述备注" v-model="dataForm.remark"/>
			</u-form-item>
		</u-form>
		<u-button @click="submit" type="primary">提交预约</u-button>
	</view>
	</view>
</template>

<script>
	import {
		getServiceTypes,
		saveServiceAppointment
	} from '@/api/community/serviceDetails.js'
	export default {
		data() {
			return {
				dataForm: {
					appointmentDate: '',
					appointmentTime: '',
					typeName: '',
					serviceTypeId: '',
					remark: '',
				},
				params: {
					year: true,
					month: true,
					day: true,
					hour: true
				},
				timeShow: false,
				typeShow: false,
				serviceTypeOptions: []
			}
		},
		onLoad() {
			this.getServiceTypeOptions()
		},
		methods: {
			timeConfirm(e) {
				this.dataForm.appointmentDate = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':00:00'
				this.dataForm.appointmentTime = e.year + '年' + e.month + '月' + e.day + '日' + e.hour + '时'
				console.log(new Date(e.year, e.month, e.day, e.hour))
			},
			typeConfirm(e) {
				console.log('type:' + this.serviceTypeOptions[e].typeName)
				this.dataForm.typeName = this.serviceTypeOptions[e].typeName
				this.dataForm.serviceTypeId = this.serviceTypeOptions[e].id
			},
			// 获取社区服务类型下拉列表
			getServiceTypeOptions() {
				getServiceTypes().then(res => {
					this.serviceTypeOptions = res.data
					console.log(this.serviceTypeOptions)
				})
			},
			submit() {
				console.log(this.dataForm)
				saveServiceAppointment(this.dataForm).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '预约成功！',
							icon: 'success'
						});
						uni.switchTab({
							url: '/pages/home/home'
						});
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'error'
						});
					}
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;

		.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		}
	}
</style>